package it.polimi.travelDream.ejb.Cthulhu;

import it.polimi.travelDream.entities.User;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class Cthulhu {
	
	private final EntityManagerFactory factory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
	private final EntityManager userDatabase=factory.createEntityManager();
	
			/**
			 * This method retrieves an User taking is user and password
			 * 
			 * */
		        
			public List<User> grandiantichi(String username, String my_password){
				System.out.println("Sono in grandiantichi");
				List<User> utentilist = null;
				
				
					String query="select u from User u where u.username="+username+" and u.password="+my_password;
					//utentilist= userDatabase.createQuery(query).getResultList();
					TypedQuery<User> lQuery =  userDatabase.createQuery(query, User.class);
					System.out.println("ho generato la lista");
		        	
				
				
				    return utentilist;	
	        
	        }
	
	
	
		      private static String convToHex(byte[] data) {
              StringBuilder buf = new StringBuilder();
              for (int i = 0; i < data.length; i++) {
                  int halfbyte = (data[i] >>> 4) & 0x0F;
                  int two_halfs = 0;
                  do {
                      if ((0 <= halfbyte) && (halfbyte <= 9))
                          buf.append((char) ('0' + halfbyte));
                      else
                      	buf.append((char) ('a' + (halfbyte - 10)));
                      halfbyte = data[i] & 0x0F;
                  } while(two_halfs++ < 1);
              }
              return buf.toString();
          }

		      /**
		  	 * 
		  	 *@author Filipazzi,Crisciullo
		  	 *This method generates the SHA-512 codification of a string
		  	 */
		      
          public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException  {
          MessageDigest md = MessageDigest.getInstance("SHA-1");
          byte[] sha1hash = new byte[40];
          md.update(text.getBytes("iso-8859-1"), 0, text.length());
          sha1hash = md.digest();
          return convToHex(sha1hash);
          }
      	
          


          /**
           * Ottiene il Context necessario per avere accesso ai RemoteSessions.
           * @return Context utilizzato per il lookup.
           * @throws NamingExeption in caso non ottenimento del Context
           */
         
                  public static InitialContext getInitialContext() throws NamingException {
                          Properties env = new Properties();
                          env.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                          env.setProperty(Context.PROVIDER_URL, "localhost:1099");
                          env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming");
                          return new InitialContext(env);
                  
          }
          
          
		          
}

	
	
	

